package com.biostime.qdingding.modules.qd.web;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Center;
import com.biostime.qdingding.modules.qd.entity.Member;
import com.biostime.qdingding.modules.qd.entity.MemberCoupon;
import com.biostime.qdingding.modules.qd.service.CenterService;
import com.biostime.qdingding.modules.qd.service.MemberCouponService;
import com.biostime.qdingding.modules.qd.service.MemberService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 普通课程Controller
 * @author king
 * @version 2016-02-17
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/memberCoupon")
public class MemberCouponController extends BaseController {

	@Autowired
	private MemberCouponService memberCouponService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CenterService centerService;
	
	@ModelAttribute
	public MemberCoupon get(@RequestParam(required=false) String id) {
		MemberCoupon entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = memberCouponService.get(id);
		}
		if (entity == null){
			entity = new MemberCoupon();
		}
		return entity;
	}
	
	@RequestMapping("getcount")
	public void getCount(MemberCoupon memberCoupon,HttpServletRequest request,HttpServletResponse response){
		if(!StringUtils.isEmpty(request.getParameter("mobile"))){
			Member member = memberService.getByMobile(request.getParameter("mobile"));
			if(member == null){
				return;
			}
			memberCoupon.setCouponId(Integer.parseInt(request.getParameter("couponId")));
			memberCoupon.setMemberId(Integer.parseInt(member.getId()));
			int count = memberCouponService.getCount(memberCoupon);
			try {
				response.getWriter().write(count+"");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@RequestMapping(value="grantSave")
	public String grantSave(MemberCoupon memberCoupon, Model model, RedirectAttributes redirectAttributes) {
		
		User user = UserUtils.getUser();
		Center center = centerService.get(user.getCenter());
		String mobile = memberCoupon.getMember().getMobile();
		Member member = memberService.getByMobile(memberCoupon.getMember().getMobile());
		if(member != null){
			memberCoupon.setMemberId(Integer.valueOf(member.getId()));
			memberCoupon.setCreateBy(user);
			memberCoupon.setUpdateBy(user);
			memberCoupon.setCreateDate(new Date());
			memberCoupon.setUpdateDate(memberCoupon.getCreateDate());
			memberCoupon.setDelFlag("0");
			memberCoupon.setIsUsed("0");
			memberCouponService.save(memberCoupon);
		}else{
			member = new Member();
			member.setMobile(mobile);
			member.setName(mobile.substring(mobile.length()-4, mobile.length()));
			member.setNikename(mobile.substring(mobile.length()-4, mobile.length()));
			member.setLoginFlag("0");
			member.setAreaId(Integer.parseInt(center.getArea().getId()));
			member.setProviceId(Integer.parseInt(center.getProvince().getId()));
			member.setCityId(Integer.parseInt(center.getCity().getId()));
			member.setCreateBy(UserUtils.getUser());
			member.setCreateDate(new Date());
			member.setUpdateBy(UserUtils.getUser());
			member.setUpdateDate(new Date());
			member.setDelFlag("0");
			memberService.apiInsert(member);
			member = memberService.getByMobile(mobile);
			memberCoupon.setMemberId(Integer.parseInt(member.getId()));
			memberCoupon.setCreateBy(user);
			memberCoupon.setUpdateBy(user);
			memberCoupon.setCreateDate(new Date());
			memberCoupon.setUpdateDate(memberCoupon.getCreateDate());
			memberCoupon.setDelFlag("0");
			memberCoupon.setIsUsed("0");
			memberCouponService.save(memberCoupon);
		}
		
		MemberCoupon memberCoup = new MemberCoupon();
		memberCoup.setCouponId(memberCoupon.getCouponId());
		memberCoup.setMember(member);
		memberCoup.setMemberId(Integer.parseInt(member.getId()));
		int count = memberCouponService.getCount(memberCoupon);
		model.addAttribute("memberCoupon",memberCoup);
		model.addAttribute("message","该用户已有此优惠券:"+count+"张");
		return "modules/qd/grantDialog";
	}
	
	@RequiresPermissions("qd:memberCoupon:view")
	@RequestMapping(value = {"list", ""})
	public String list(MemberCoupon memberCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MemberCoupon> page = memberCouponService.findPage(new Page<MemberCoupon>(request, response), memberCoupon); 
		model.addAttribute("page", page);
		return "modules/qd/memberCouponList";
	}

	@RequiresPermissions("qd:memberCoupon:view")
	@RequestMapping(value = "form")
	public String form(MemberCoupon memberCoupon, Model model) {
		model.addAttribute("memberCoupon", memberCoupon);
		return "modules/qd/memberCouponForm";
	}

	@RequiresPermissions("qd:memberCoupon:edit")
	@RequestMapping(value = "save")
	public String save(MemberCoupon memberCoupon, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, memberCoupon)){
			return form(memberCoupon, model);
		}
		memberCouponService.save(memberCoupon);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/memberCoupon/?repage";
	}
	
	@RequiresPermissions("qd:memberCoupon:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, MemberCoupon memberCoupon, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				memberCouponService.delete(new MemberCoupon(id));
			}
		}else{
			memberCouponService.delete(memberCoupon);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/memberCoupon/?repage";
	}

}